package com.ijiuchuang.jc_localization.presenter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.PointF;

import com.ijiuchuang.jc_localization.bean.event.SocketGetInfoEvent;
import com.ijiuchuang.jc_localization.contract.LocationContract;
import com.ijiuchuang.jc_localization.model.LocationModel;
import com.ijiuchuang.jc_localization.tools.utils.Log;

import rx.functions.Action1;

/**
 * Created by Veev on 2016/6/2
 *
 * @link 384506557@qq.com
 */
public class LocationPresenter implements LocationContract.Presenter {

    private Context mContext;
    private LocationContract.View mainView;
    private LocationContract.Model mainModel;

    public LocationPresenter(Context mContext, LocationContract.View mainView) {
        this.mContext = mContext;
        this.mainView = mainView;
        this.mainModel = new LocationModel();

        mainView.setPresenter(this);
    }

    @Override
    public void start() {
        // start here
        loadConfig();
    }

    @Override
    public void socketConnect() {
        mainModel.socketConnect(new LocationContract.Model.DataComingListener() {
            @Override
            public void onCame(SocketGetInfoEvent event) {
                locationChanged(event);
            }
        });
    }

    @Override
    public void locationChanged(SocketGetInfoEvent event) {
        mainModel.locationProcess(event, new LocationContract.Model.LocationChangeListener() {
            @Override
            public void onChanged(PointF point) {
                mainView.updatePos(point);
            }

            @Override
            public void onChanged(String mac, String name, PointF point) {
                mainView.updatePos(mac, name, point);
            }
        });
    }

    @Override
    public void getPoint() {
        mainView.setPoint(mainModel.getPoint());
    }

    @Override
    public void loadConfig() {
        mainModel.loadMap(mContext, new Action1<Bitmap>() {
            @Override
            public void call(Bitmap bitmap) {
                mainView.setMap(bitmap);
            }
        });
    }

    @Override
    public void onDestroy() {
        if (mainView != null) {
            mainView = null;
        }
    }
}